import 'package:event_bus/event_bus.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:ysy_flutter_mvvm_yuanxuan/event/BottomNavigationBarIndex.dart';
import 'package:ysy_flutter_mvvm_yuanxuan/event/ToBottomNavigationBarIndex.dart';
import 'package:ysy_flutter_mvvm_yuanxuan/helper/AppProperties.dart';
import 'package:ysy_flutter_mvvm_yuanxuan/helper/Constant.dart';
import 'package:ysy_flutter_mvvm_yuanxuan/helper/ImageHelper.dart';
import 'package:ysy_flutter_mvvm_yuanxuan/helper/RouteHelper.dart';
import 'package:ysy_flutter_mvvm_yuanxuan/view/main/video/Creation.dart';

import 'main/home/Home.dart';
import 'main/industry/Industry.dart';
import 'main/mine/Mine.dart';

EventBus eventBus = new EventBus();

class AppMain extends StatefulWidget {

  @override
  _AppMainState createState() => _AppMainState();
}

class _AppMainState extends State<AppMain> {

  @override
  Widget build(BuildContext context) {
    return WillPopScope(
      onWillPop:(){
        return Future.value(false);
      },
      child:ButtonWidget(),
    );
  }
}

class ButtonWidget extends StatefulWidget {

  List<Widget> _list = [Home(), CreationWidget(), Industry(), MineWidget()];

  @override
  _ButtonWidgetState createState() => _ButtonWidgetState();

}

GlobalKey<_ButtonWidgetState> mBottomNavigationBarKey = GlobalKey();

int tabIndex=0;

class _ButtonWidgetState extends State<ButtonWidget> {

  _changIndex(index) {
    if((index==3||index==1) && !isLogin()){  //判斷是否登錄
      toLoginWidget(context);
    }else{
      setState(() {
        tabIndex = index;
        eventBus.fire(BottomNavigationBarIndex(tabIndex));
      });
    }
  }

  @override
  void initState() {
    super.initState();
    eventBus.on().listen((event){
      if(event is ToBottomNavigationBarIndex){
        setState(() {
          tabIndex = event.tabIndex;
          eventBus.fire(BottomNavigationBarIndex(tabIndex));
        });
      }
    });
  }

  @override
  Widget build(BuildContext context) {
    return  Scaffold(
      body: IndexedStack(index: tabIndex,children:widget._list),
      bottomNavigationBar: BottomNavigationBar(
          key:mBottomNavigationBarKey,
          type: BottomNavigationBarType.fixed,
          selectedItemColor: Color(ConstantColors.mainColor),
          unselectedItemColor: Colors.black45,
          iconSize: 20,
          showUnselectedLabels: true,
          unselectedFontSize: 12,
          selectedFontSize: 12,
          currentIndex: tabIndex,
          onTap: (index) => _changIndex(index),
          backgroundColor: Colors.white,
          items: [
            BottomNavigationBarItem(
                tooltip: "",
                label: "产业",
                icon: Padding(
                  padding: const EdgeInsets.all(3.0),
                  child: ImageIcon(AssetImage(assetPath("ic_tab_chanye_def.png"))),
                )),
            BottomNavigationBarItem(
                tooltip: "",
                label: "创作",
                icon: Padding(
                  padding: const EdgeInsets.all(3.0),
                  child:
                  ImageIcon(AssetImage(assetPath("ic_tab_chuangzuo_def.png"))),
                )),
            BottomNavigationBarItem(
                tooltip: "",
                label: "工业",
                icon: Padding(
                  padding: const EdgeInsets.all(3.0),
                  child: ImageIcon(AssetImage(assetPath("ic_tab_gongye_def.png"))),
                )),
            BottomNavigationBarItem(
                tooltip: "",
                label: "我的",
                icon: Padding(
                  padding: const EdgeInsets.all(3.0),
                  child: ImageIcon(AssetImage(assetPath("ic_tab_my_def.png"))),
                )),
          ]),
    );
  }
}
